#!/bin/bash

static_payload="Payload"
static_provision="embedded.mobileprovision"
static_result="result_log"

#-e可以使用tab补全
read -e -p "输入路径:" dir
if [ -z "${dir}" ]; then
        echo "Error:目标路径为空，任务结束"
    exit 0
fi

path=${dir%/*}
orig_ipa=${dir##*/}
if [ -z "${path}" -o -z "${orig_ipa}" ]; then
    echo "Error:路径解析错误，任务结束"
    exit 0
fi

cd "${path}"
if [ -e ${orig_ipa} ]; then
    cpoy_ipa="${orig_ipa%%.*}.zip"
    app_ipa="${orig_ipa%%.*}.app"
    #创建log
    touch ${static_result}
    provision_path="${static_payload}/${app_ipa}"
    cp ${orig_ipa} ${cpoy_ipa}
    unzip -q ${cpoy_ipa}
    cd ${provision_path}
    security cms -D -i embedded.mobileprovision >> "../../${static_result}"
    #删除
    cd ..
    cd ..
    rm ${cpoy_ipa}
    rm -r ${static_payload}
else
    echo "Error:.ipa文件不存在，任务结束"
    exit 0
fi



